package com.goatstone.client;

import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;

import com.google.gwt.user.client.ui.Label;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
/* store FontSizeOption in FontSizeOptions
 * with size as key ?
 * as array
 * setDisplay 
 * keeps track of options
 * turn the appropriate size panel on
 * turn the others off
 * call 
 * setSelectedFontSize() 
 * on init
 * and onClick
 *  */
class FontSizePicker extends Composite{
//	private int selectedFontSize;
	private HorizontalPanel optionsPanel;
	private VerticalPanel mainPanel;
//	protected Label label;
	MainWidgetComposite mwc;
	
	private FontSizeOption selectedFontSizeOption;
	private FontSizeOption previouslySelectedFontSizeOption;

	FontSizePicker(MainWidgetComposite mwc){
		this.mwc = mwc;
		mainPanel = new VerticalPanel();
		DOM.setElementAttribute(mainPanel.getElement(), "id", "goat-fontSizePicker");

//		label = new Label("Font Size");
		optionsPanel = new HorizontalPanel(); 
		generateFontSizeOptionsPanel();
		
//		mainPanel.add(label);
		mainPanel.add(optionsPanel);

		setDisplay();

		initWidget(mainPanel);
	}
	public int getSelectedFontSize(){			
		return selectedFontSizeOption.size;		
	}
	public void setDisplay(){
		DOM.setStyleAttribute( selectedFontSizeOption.getElement(),"backgroundColor","rgb(255,255,255)");
		if(previouslySelectedFontSizeOption != null){
			DOM.setStyleAttribute( previouslySelectedFontSizeOption.getElement(),"backgroundColor","rgb(150,150,150)");
		}	
		mwc.setFeedBack("Font Size: "+getSelectedFontSize()+" pixels");
		mwc.setTextBoxFocus();
	}
//	private FontSizeOption getSelectedFontSizeOption(){
//		return selectedFontSizeOption;
//	}	
	private void generateFontSizeOptionsPanel(){
		for(int i=20; i<60; i+=10){
			FontSizeOption fontSizeOption = new FontSizeOption(i);
			if(i==20){			
				selectedFontSizeOption = fontSizeOption;
			}
			optionsPanel.add(fontSizeOption);
		}
	}
	private class FontSizeOption extends Widget {  
		private HandlerRegistration registration;  	
		private final DivElement element;
		private  int size;
		FontSizeOption( int size ) {  
			this.size = size;
			element = Document.get().createDivElement();  			   
			element.setInnerText("f");  
			setElement(element);  
			setStylePrimaryName("fontSizeOption");
			element.getStyle().setProperty("fontSize", size+"px");

			element.getStyle().setProperty("backgroudColor", "rgb(150,150,150)");
		}  			   		
		@Override  
		protected void onLoad() {  
			super.onLoad();  			   
			registration = addDomHandler(new ClickHandler() {  
				public void onClick(final ClickEvent event) { 
					previouslySelectedFontSizeOption = selectedFontSizeOption;
					selectedFontSizeOption  = (FontSizeOption) event.getSource();
					setDisplay();
				}  
			}, ClickEvent.getType());  
		}  			   
		@Override  
		protected void onUnload() {  
			super.onUnload();  			   
			registration.removeHandler();  
			registration = null;  
		}  			   
	}  	
}
